<!-- 我的日志 -->
<template>
  <div class="my-record">
    <!-- 顶部标题栏组件 -->
    <top-header :leftArrow="leftArrow"></top-header>

    <!-- 日志清单 -->
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell v-for="(item, index) in logList" :key="index" :value="item.Info" @click="toDetail(item)">
        <template #title>
          <van-tag plain type="primary" style="margin-right: 2px;" color="burlywood">{{ index + 1 }}</van-tag>
          <span class="custom-title">{{ item.CreateTime | filterTime }}</span>
        </template>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { TopHeader } from '@/components'
import { getApiLog } from '@/request/api'
export default {
  name: 'MyRecord',
  components: {
    TopHeader
  },
  filters: {
    filterTime (val) {
      return val.slice(0, 10)
    }
  },
  data () {
    return {
      leftArrow: true, // 顶部标题栏组件是否有返回按钮
      logList: [], // 我的日志数据
      logCount: 0, // 我的日志数据总量
      loading: false,
      finished: false,
      pageIndex: 0,
      userId: sessionStorage.getItem('userId') // 用户id
    }
  },
  created () {
    const param = { id: this.userId, 'page': this.pageIndex, 'pageCount': 10 }
    // 获取我的日志数据
    getApiLog(param).then(res => {
      console.log(res)
      this.logList = res.result.apiLog
      this.logCount = res.result.count
    })
  },
  methods: {
    onLoad () {
      // 异步更新数据
      setTimeout(() => {
        this.pageIndex++
        const param = { 'id': this.userId, 'page': this.pageIndex, 'pageCount': 10 }
        getApiLog(param).then(res => {
          this.logList = [...this.logList, ...res.result.apiLog]
        })
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        if (10 * (this.pageIndex + 1) >= this.logCount) {
          this.finished = true
        }
      }, 1000)
    },
    // 点击事件触发,页面跳转到详情页
    toDetail (value) {
      console.log('跳转到详情页', value)
      sessionStorage.setItem('LOG_LIST', JSON.stringify(value))
      this.$router.push({ path: '/recordDetail' })
    }
  }
}
</script>

<style scoped lang="less">
.my-record {
  width: 100%;
  height: 100%;
  .van-list {
    overflow-y: auto;
    overflow-x: hidden;
    height: calc(100% - 1.2rem);
    .van-cell {
      .van-cell__title {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .van-cell__value {
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>
